package com.topcycle.tlight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

/**
 * Created by Administrator on 15-9-9.
 */
public class DevInfoEdit extends Activity{

    public static final String RENAME_OK = "rename_ok";
    public static final String DEVICE_DEL = "device_del";
    public static final String SWITCH_MODE = "SWITCH_MODE";

    public DevInfoEdit() {

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dev_info);

        final ImageView editCancel = (ImageView) findViewById(R.id.dev_edit_cancel);
        final ImageView editCheck = (ImageView) findViewById(R.id.dev_edit_check);
        final EditText editName = (EditText) findViewById(R.id.dev_edit_name);
        final Button delBtn = (Button) findViewById(R.id.dev_del);
        final Button modeBtn = (Button) findViewById(R.id.dev_switchmode);
        final Device dev ;

        Bundle bundle = this.getIntent().getExtras();
        final int pos = bundle.getInt(MainActivity.BTN_DEVICE_INFO);
        System.out.println(pos);
        dev = DeviceManager.getCurrentDevice();
        if (dev == null) {
            DevInfoEdit.this.finish();
            return;
        }
        editName.setText(dev.name);
        editCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DevInfoEdit.this.finish();
            }
        });

        editCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeviceManager.renameDevice(dev.mac,dev.ip,editName.getText().toString());
                Intent intent = new Intent();
                intent.putExtra(MainActivity.ACTIVITY_RESULT,RENAME_OK);
                setResult(RESULT_OK,intent);
                DevInfoEdit.this.finish();
            }
        });

        delBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                NetController.sendDelLedCmd();
                DeviceManager.delDeviceByIndex(pos);
                DeviceManager.updateCurrentDeviceByPosition(0);
                Intent intent = new Intent();
                intent.putExtra(MainActivity.ACTIVITY_RESULT,DEVICE_DEL);
                setResult(RESULT_OK, intent);
                DevInfoEdit.this.finish();
            }
        });

        modeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NetController.sendSwitchModeCmd();
                Intent intent = new Intent();
                intent.putExtra(MainActivity.ACTIVITY_RESULT,SWITCH_MODE);
                setResult(RESULT_OK, intent);
                DevInfoEdit.this.finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        DevInfoEdit.this.finish();
    }

    @Override
    public void finish() {
        super.finish();
    }
}
